package com.test.demo;

import java.util.Scanner;
import java.util.Random;

public class ArrayTest6 {
    int[][] games; // 游戏面板
    int emptyRow; // 空白位置
    int emptyCol; // 空白位置
    Scanner player; // 玩家输入

    public ArrayTest6() {
        games = new int[4][4];
        player = new Scanner(System.in);
    }

    public void start() {
        gamePanel();// 初始化游戏面板
        randomGame();// 随机打乱游戏
        int elect;
        do {
            menu();
            System.out.print("请输入你的选择：");
            elect = player.nextInt();
            switch (elect) {
                case 1:
                    playGame();
                    break;
                case 2:
                    System.out.println("欢迎再次游玩！");
                    break;
                default:
                    System.out.println("输入有误，请重新输入");
            }
        } while (elect != 2);
    }

    void menu() {
        System.out.println("石头迷阵游戏");
        System.out.println("1.开始游戏");
        System.out.println("2.退出游戏");
    }

    void gamePanel() {
        int num = 1;
        for (int i = 0; i < games.length; i++) {
            for (int j = 0; j < games[0].length; j++) {
                games[i][j] = num++;
            }
        }
        games[games.length - 1][games[0].length - 1] = 0; // 最后一个位置为空
        emptyRow = games.length - 1;// 取空白位置的行
        emptyCol = games[0].length - 1;// 取空白位置的列
    }

    void randomGame() {
        Random random = new Random();
        for (int i = 0; i < 100; i++) { // 随机移动100次
            int direction = random.nextInt(4);// 0: 上, 1: 下, 2: 左, 3: 右
            switch (direction) {
                case 0: // 上
                    if (emptyRow > 0) {
                        swap(emptyRow, emptyCol, emptyRow - 1, emptyCol);
                        emptyRow--;
                    }
                    break;
                case 1: // 下
                    if (emptyRow < games.length - 1) {
                        swap(emptyRow, emptyCol, emptyRow + 1, emptyCol);
                        emptyRow++;
                    }
                    break;
                case 2: // 左
                    if (emptyCol > 0) {
                        swap(emptyRow, emptyCol, emptyRow, emptyCol - 1);
                        emptyCol--;
                    }
                    break;
                case 3: // 右
                    if (emptyCol < games[0].length - 1) {
                        swap(emptyRow, emptyCol, emptyRow, emptyCol + 1);
                        emptyCol++;
                    }
                    break;
            }
        }
    }

    void swap(int row1, int col1, int row2, int col2) {
        int temp = games[row1][col1];
        games[row1][col1] = games[row2][col2];
        games[row2][col2] = temp;
    }

    void playGame() {
        System.out.println("游戏开始！");
        printGameBoard();
        while (!isSolved()) {
            System.out.println("请输入移动方向（w: 上, s: 下, a: 左, d: 右）:");
            char move = player.next().charAt(0);// 获取玩家输入的字符
            switch (move) {
                case 'w':
                    if (emptyRow > 0) {
                        swap(emptyRow, emptyCol, emptyRow - 1, emptyCol);
                        emptyRow--;
                    }
                    break;
                case 's':
                    if (emptyRow < games.length - 1) {
                        swap(emptyRow, emptyCol, emptyRow + 1, emptyCol);
                        emptyRow++;
                    }
                    break;
                case 'a':
                    if (emptyCol > 0) {
                        swap(emptyRow, emptyCol, emptyRow, emptyCol - 1);
                        emptyCol--;
                    }
                    break;
                case 'd':
                    if (emptyCol < games[0].length - 1) {
                        swap(emptyRow, emptyCol, emptyRow, emptyCol + 1);
                        emptyCol++;
                    }
                    break;
                default:
                    System.out.println("无效的移动，请重新输入");
            }
            printGameBoard();
        }
        System.out.println("恭喜你，游戏胜利！");
    }

    boolean isSolved() {// 判断游戏是否结束
        int num = 1;
        for (int i = 0; i < games.length; i++) {
            for (int j = 0; j < games[0].length; j++) {
                if (i == games.length - 1 && j == games[0].length - 1) {
                    return games[i][j] == 0;
                }
                if (games[i][j] != num++) {
                    return false;
                }
            }
        }
        return true;
    }

    void printGameBoard() {
        for (int i = 0; i < games.length; i++) {
            for (int j = 0; j < games[0].length; j++) {
                if (games[i][j] == 0) {
                    System.out.print("   ");
                } else {
                    System.out.print(games[i][j] + " ");
                }
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        ArrayTest6 game = new ArrayTest6();// 创建游戏对象
        game.start();// 开始游戏
    }
}